package com.kingyea.mobilepolice.systemsync;

import com.alibaba.fastjson.JSON;
import com.kingyea.mobilepolice.system.StateCode;
import com.kingyea.mobilepolice.system.exception.BusinessException;
import com.kingyea.mobilepolice.systemsync.bean.InvokedMethod;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 把需要同步的系统操作发送到mq
 *
 * @author Mr.Lin
 * @version v1.0.0
 * @description
 * @date Created in 2018/3/1 16:31
 */
@Component
//@ConfigurationProperties( prefix = "spring.activemq")
@Aspect
public class Send2QueueAspect2 {

    public static Pattern pattern = Pattern.compile("(?<=\\()[^\\)]+");


    @SuppressWarnings("unchecked")
    @After(value = "@annotation(Send2Queue2)", argNames = "JoinPoint,Send2Queue2")
    public void sendEvent(final JoinPoint joinPoint, final Send2Queue2 send2Queue2) throws Exception {
        System.out.println(" 拦截开始 '''''''''''''''''''''''''''''''''''''''''");

        {
            final InvokedMethod invokedMethod = new InvokedMethod();


            // 1.获取操作的当前类名 获取切面的class名
            final String targetClassName = joinPoint.getTarget().getClass().getCanonicalName();


            invokedMethod.setClazzName(targetClassName);

            // 2.获取操作的方法名
            final String methodName = joinPoint.getSignature().getName();
            invokedMethod.setMethodName(methodName);
            // 3.获得参数列表
            final Object[] arguments = joinPoint.getArgs();
            // 4.获得参数个数
            invokedMethod.setAttributeNumber(arguments.length);
            // 5.给传递参数赋值
            final Object[] params = new Object[arguments.length];
            for (int i = 0; i < arguments.length; i++) {
                params[i] = arguments[i];
            }
            invokedMethod.setParams(params);

            final List<String> typesStrList = new ArrayList<>();
            Matcher matcher = pattern.matcher(joinPoint.getSignature().toLongString());
            while (matcher.find()) {
                String typesStr = matcher.group();
                if (typesStr.indexOf(",") != -1) {
                    String[] typeStrs = typesStr.split(",");
                    for (String typeStr : typeStrs) {
                        typesStrList.add(typeStr);
                    }
                } else {
                    typesStrList.add(typesStr);
                }
            }
            invokedMethod.setTypeStrList(typesStrList);
            if (invokedMethod.getAttributeNumber().intValue() != invokedMethod.getTypeStrList().size()) {
                throw new BusinessException(StateCode.SYSTEM_ERROR, "拦截到的service有误,传入参数数量与形参数量不一致!!! ");
            }
            System.out.println("invokedMethod:");
            System.out.println(JSON.toJSONString(invokedMethod));


            SyncInvokMethodProxy2 syncInvokMethodProxy2 = new SyncInvokMethodProxy2();
            syncInvokMethodProxy2.invokService(invokedMethod);
        }


    }

}




